const strategies = {
  isNonEmpty(value, errorMsg) {
    if (value === "") {
      return errorMsg;
    }
  },
  minLength(value, length, errorMsg) {
    if (value.length < length) {
      return errorMsg;
    }
  }
};

class Validator {
  constructor() {
    this.cache = [];
  }
  add(value, rules) {
    var self = this
   for(let i =0,rule;rule = rules[i++];){
     (function(rule){
      var strategyAry = rule.strategy.split(':')
      let errorMsg = rule.errorMsg
      self.cache.push(function(){
        var strategy = strategyAry.shift()
        strategyAry.unshift(value)
        strategyAry.push(errorMsg)
        return strategies[strategy].apply(value,strategyAry)
      })
     })(rule)
   }
  }
  start() {
    for (let i = 0, validataFun; (validataFun = this.cache[i++]); ) {
      var errorMsg = validataFun();
      if (errorMsg) {
        return errorMsg;
      }
    }
  }
}

export default Validator